/**
*	插件：流程模块详情页
*	开发者：雨中磐石
*	地址：www.rockoa.com
*	日期：2023-07-17
*/

js.xiang = {
	init:function(){
		
	},
	create:function(cans){
		var me = this;
		var sw = (winWb()>500) ? 120 :85;
		var defv = js.applyIf(cans,{
			rand:js.getrand(),
			data:{}, //表单数据
			num:'',  //模块编号
			mid:0,
			isflow:0,
			view:'',
			cnum:'',
			funitid:'',
			moders:{},
			filedata:[],
			subdata:{},
			ismodal:false,
			store:{},
			modejs:'', //详情js
			saveurl:'',
			ispl:true,
			logcount:0,
			tdleft:'width:'+sw+'px;color:#555555;text-align:right;padding:3px 2px;',
			fieldsarr:[],
			onxiangview:function(){return '';},
			onreplace:function(d){return d;},
			fileshow:{}
		});
		for(var i in defv)this[i]=defv[i];
		var rand  = this.rand;

		this.init = function(){
			if(this.num && this.modejs){
				var url = 'res/flow/'+this.num+'.js';
				js.importjs(url, function(){
					me.lurjsshow();
				});
			}else{
				this.initshow();
			}
		};
		
		this.initshow = function(){
			if(this.view){
				$('#'+this.view+'').html(this.showxiang());
				if(this.isflow > 0)importjs('fcheck',function(){me.initcheck();});
				js.initbtn(this, this.view);
			}
			this.chulifile();
		};
		
		this.lurjsshow = function(){
			var key  = ''+this.num+'_xiang';
			if(js[key]){
				var nobj = new js[key]();
				for(var i in nobj)this[i] = nobj[i];
				if(this.initbodys && this.view)this.initbodys();
			}
			this.initshow();
		};
		
		//布局
		this.xiangview = function(){
			var s = this.onxiangview();
			if(s)return s;
			var str = '';
			var farr   		= this.fieldsarr,i,d,val,fid,type;
			str += '<table width="100%">';
			for(i=0;i<farr.length;i++){
				d 	= farr[i];
				fid = d.fields;
				type = d.fieldstype;
				if(d.iszs==1){
					str+='<tr>';
					if(type=='subdata'){
						str+='<td colspan="3" style="padding:3px;">{'+fid+'}</td>';
					}else{
						str+='<td><div style="'+this.tdleft+'">^'+fid+'^</div></td>';
						str+='<td style="color:#cccccc">:</td>';
						str+='<td width="90%" style="padding:3px;">{'+fid+'}</td>';
					}
					str+='</tr>';
				}
			}
			str +='</table>';
			return str;
		}
		
		this.showxiang = function(){
			var farr = this.fieldsarr,i,d,val,str='',cls='',type,fid,col;
			if(!this.ismodal && !js.web.ismobile() && winWb()>500)cls='rock-border rock-radius';
			var cont = this.xiangview();
			
			var da   = this.onreplace(this.data);
			for(i=0;i<farr.length;i++){
				d 	 = farr[i];
				fid  = d.fields;
				type = d.fieldstype;
				cont = cont.replace('^'+fid+'^', d.name);
				
				val  = da[fid];
				if(da[fid+'_textcn'])val = da[fid+'_textcn'];
				if(typeof(val)=='undefined')val='';
				if(type=='uploadimg' && val)val='<img src="'+val+'" onclick="js.viewimg(this)" height="100">';
				if(type=='uploadfile' && val){
					this.fileshow[fid] = val;
					val = '<span id="uploadfile_'+fid+'_'+rand+'_view"></span>';
				}
				if(type=='subdata'){
					val = '<span id="subdataview_'+fid+'_'+rand+'"></span>';
				}
				if(type=='textarea' && val){
					val = val.replace(/\n/gi,'<br>');
				}
				col = this.data[fid+'_color'];
				if(col && val){
					val = '<font color="'+col+'">'+val+'</font>';
				}
				cont = cont.replace('{'+fid+'}', val);
			}
			
			str  = '<div style="max-width:600px;margin:10px 0px" align="left">';
			str += '<div class="rock-panel '+cls+'" style="padding:10px 20px;position:relative">';
			
			str += '<div style="position:absolute;top:2px;left:2px"><a class="fontsize12" clickevt="clickopt">&nbsp;操作<i class="icon-angle-down"></i>&nbsp;</a></div>';
			
			str += '<div id="contentall_'+rand+'">';
			str += cont;
			//str += ' <div style="position:absolute;bottom:10px;right:10px"><img height="100" width="100" src="http://www.rockoa.com/images/gz.png"></div>';
			str += '</div>';
			
			str += '</div>';
			
			if(this.isflow > 0){
				str+='<div class="hui fontsize14" style="padding:8px 0px 3px 10px;">流程审批</div>';
				str+='<div class="rock-panel '+cls+'" id="checkdiv_'+rand+'" style="padding:10px 20px"></div>';
			}
			
			if(this.ispl == 1){
				str+='<div class="hui fontsize14" style="padding:8px 0px 3px 10px;">评论</div>';
				str+='<div class="rock-panel '+cls+'"  style="padding:10px 20px">';
				str+='	<div><textarea class="textarea" id="plcont_'+rand+'" style="flex:1;height:60px" placeholder="请输入评论内容"></textarea></div>';
				str+='	<div id="plfile_'+rand+'"></div>';
				str+='	<div><input id="plfile_'+rand+'_input" type="hidden"><button type="button" clickevt="pinglunfile" class="webbtn webbtn-mini webbtn-default">相关文件...</button></div>';
				str+='	<div style="margin-top:10px"><button class="webbtn" type="button"  clickevt="submitpl">提交评论</button></div>';
				str+='</div>';
			}
			
			//操作记录
			if(this.logcount>0){
				str+='<div clickevt="logshow" class="hui fontsize14 cursor" style="padding:8px 0px 3px 10px;">操作记录('+this.logcount+')<i class="icon-angle-down"></i></div>';
				str+='<div class="rock-panel '+cls+'" id="flowlogdiv_'+rand+'" style="display:none;border-top:.5px #eeeeee solid">';
				str+='<div style="padding:5px" align="center"><i style="height:40px;width:40px" class="rock-loading"></i></div>';
				str+='</div>';
			}
			
			str+='</div>';
			return str;
		};

		
		this.closetan = function(){
			this.optobj.closetan();
		}

		
		this.initcheck = function(){
			var cansv = new js.fcheck.create({rand:this.rand});
			for(var i in cansv)this[i]= cansv[i];
			cansv.xiangobj = this;
			var sview = 'checkdiv_'+rand+'';
			$('#'+sview+'').html(this.getcourse());
			js.initbtn(this, sview);
		}
		
		this.chulifile = function(){
			var fid;
			if(this.subdata)for(fid  in this.subdata){
				this.xiangshow(this.subdata[fid],fid);
			}
			
			var len = this.filedata.length;
			if(len>0)importjs('rockfile','create', function(){
				me.fileviewall();
			});
			
		}
		this.fileviewall = function(){
			js.rockfile.setfiledata(this.filedata);
			var fid,vid;
			for(fid in this.fileshow){
				vid = 'uploadfile_'+fid+'_'+rand+'_view';
				$('#'+vid+'').html('');
				js.rockfile.fileviewshow(this.fileshow[fid], vid);
			}
		}
		
		this.formdisabled=function(){
			$(document[this.formname]).find('*').attr('disabled', true);
		}
		
		this.form = function(fid){
			return jsform(fid, this.formname);
		}
		
		this.clickopt = function(o1){
			importjs('menu', 'create', {
				data:[{name:'打印...',lx:0},{name:'另存图片...',lx:0}],
				obj:$(o1),
				onclick:function(d){
					me.clickoptclick(d);
				}
			});
		}
		
		this.clickoptclick = function(d){
			var lx = d.lx;
			if(lx==0){
				var cans = {url:'admin/print',name:this.moders.name,params:{title:this.moders.name,contid:'contentall_'+rand+''}};
				xcy.openkey(cans)
			}
		}
		
		this.submitpl = function(o1){
			var val = get('plcont_'+rand+'').value;
			if(!val){
				js.show.error('请输入评论内容');
				return;
			}
			js.ajax.post({
				url:'api/flow/checkping?num='+this.num+'&mid='+this.mid+'',
				cnum:this.cnum,
				funitid:this.funitid,
				btnobj:o1,
				data:{
					content:val,
					filecont:get('plfile_'+rand+'_input').value
				}
			},function(ret){
				me.logshows();
			});
		}
		
		this.pinglunfile = function(){
			importjs('rockfile','uploafile',{
				name:'上传文件',
				uptype:'',
				inputobj:get('plfile_'+rand+'_input'),
				cnum:this.cnum,
				view:'plfile_'+rand+''
			});
		}
		
		
		this.logshow = function(){
			$('#flowlogdiv_'+rand+'').toggle();
			if(this.openlogbool)return;
			this.openlogbool = true;
			js.ajax.get({
				url:'api/flow/data',
				cnum:this.cnum,
				data:{
					num:'flowlog',
					page:1,
					limit:0,
					loadci:2,
					table:this.moders.table,
					mid:this.mid
				}
			},function(ret){
				me.logshowdata(ret.data);
			});
		}
		
		this.logshows	 = function(){
			$('#plfile_'+rand+'').html('');
			if(!this.openlogbool){
				this.logshow();
			}else{
				this.openlogbool = false;
				$('#flowlogdiv_'+rand+'').hide();
				this.logshow();
			}
		}
		
		this.logshowdata = function(da){
			var rows = da.rows,i,s='',d;
			this.fileshows = {};
			for(i=0;i<rows.length;i++){
				d = rows[i];
				if(!d.face)d.face = 'images/noface.png';
				s+='<div style="padding:8px 12px;border-bottom:.5px #eeeeee solid">';
				s+='<table width="100%"><tr valign="top">';
				s+='<td><img onclick="js.viewimg(this)" style="height:30px;width:30px;border-radius:50%;margin-right:5px" src="'+d.face+'"></td>';
				s+='<td width="100%">';
				s+=' <div>'+d.checkname+'['+d.name+']<font color="'+d.statuscolor+'">'+d.statusname+'</font><span class="hui fontsize14">('+d.optdt+')</span></div>';
				if(d.explain)s+=' <div>'+replacebr(d.explain)+'</div>';
				if(d.filecont){
					s+=' <div id="pinglunfile_'+d.id+'_'+rand+'"></div>';
					this.fileshows[d.id] = d.filecont;
				}
				s+='</td>';
				s+='</tr></table>';
				s+='</div>';
			}
			$('#flowlogdiv_'+rand+'').html(s);
			if(da.filedata.length>0)this.logshowfile(da.filedata, false);
		}
		
		this.logshowfile = function(da, bo){
			if(bo){
				js.rockfile.setfiledata(da);
				var fid,vid;
				for(fid in this.fileshows){
					vid = 'pinglunfile_'+fid+'_'+rand+'';
					js.rockfile.fileviewshow(this.fileshows[fid], vid);
				}
				return;
			}
			importjs('rockfile','create', function(){
				me.logshowfile(da, true);
			});
		}
		
		this.xiangshow = function(da,fields){
			var s = '',i,d,j,len,jlen,d1,fid,align='',val,type;
			var view = 'subdataview_'+fields+'_'+rand+'';
			var farr = da.fieldsarr,data = da.data,filedata = da.filedata;
			len  = data.length;
			jlen = farr.length;
			if(len==0)return;
			s+='<div class="rock-border rock-radius" style="margin-bottom:8px;padding:0px;height:auto;width:auto">';
			s+='<div style="padding:5px;background:#e1e1e1">'+da.name+'</div>';
			s+='<table width="100%" style="border-color:#e1e1e1" border=".5"><tr>';
			s+='<th></th>';
			for(j=0;j<jlen;j++){
				d1 = farr[j];
				if(d1.iszs!='1')continue;
				fid= d1.fields;
				align='center';
				if(d1.isalign=='1')align='left';
				if(d1.isalign=='2')align='right';
				s+='<th style="padding:3px;text-align:'+align+'">'+d1.name+'</th>';
			}
			s+='</tr>';
			for(i=0;i<len;i++){
				d = data[i];
				s+='<tr><td width="30" style="padding:3px" align="center">'+(i+1)+'</td>';
				for(j=0;j<jlen;j++){
					d1 = farr[j];
					if(d1.iszs!='1')continue;
					fid= d1.fields;
					type= d1.fieldstype;
					align='center';
					if(d1.isalign=='1')align='left';
					if(d1.isalign=='2')align='right';
					val = d[fid];
					if(type=='uploadfile' && val){
						fid= ''+fields+'_'+fid+''+d.id+'';
						this.fileshow[fid] = val;
						val='<span id="uploadfile_'+fid+'_'+rand+'_view">'+val+'</span>';
					}
					if(type=='uploadimg' && val)val='<img src="'+val+'" onclick="js.viewimg(this)" height="100">';
					s+='<td style="padding:3px;text-align:'+align+'">'+val+'</td>';
				}
				s+='</tr>';
			}
			s+='</table>';
			s+='</div>';
			$('#'+view+'').html(s);
			len = filedata.length;
			for(i=0;i<len;i++)this.filedata.push(filedata[i]);
		}

		this.init();
	},

	
	
	//打开详情页
	open:function(cans){
		var can   = {num:cans,mid:0,ismodal:false,name:'',tableid:'',xiangwidth:700}
		for(var i in cans)can[i]=cans[i];
		var params= can;
		var bt = '详情';
		if(can.name)bt=''+can.name+'('+bt+')';
		if(can.ismodal){
			this.openmodal(can, params, bt);
		}else{
			xcy.openkey({url:'flow/xiang','name':bt, params:params});
		}
	},
	openmodal:function(can, params, bt){
		if(get('flowxiang_'+can.num+'_main')){
			js.show.error('请勿重复打开');
			return;
		}
		js.modal.main({
			width:can.xiangwidth,
			height:200,
			bbar:'none',
			action:'flowxiang_'+can.num+'',
			title:bt,
			html:'<div id="xiangview_'+can.num+'"></div>'
		});
		xcy.addcont('flow/xiang','xiangview_'+can.num+'',params);
	},
	close:function(bh){
		js.modal.close('flowxiang_'+bh+'');
	},
	resize:function(bh){
		js.modal.resize('flowxiang_'+bh+'');
	}
}